package com.thon.controller.system;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;

/**
 * @File: ThirdPartyController.java
 * @author zemo
 * @mail chenzeming@lanbaoo.com
 * @date: Feb 10, 2014 4:00:26 PM
 * @Description: TODO
 */
@Controller
@RequestMapping("/third")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class ThirdPartyController {

	@RequestMapping(value = "/get_user_info", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> qqGetUserInfo(HttpServletRequest request) throws ClassNotFoundException {

		String url = request.getParameter("url");
		System.out.println(url);
		HttpClient httpClient = new HttpClient();
		// 创建GET方法的实例
		GetMethod getMethod = new GetMethod(url);
		// 使用系统提供的默认的恢复策略
		getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		try {
			// 执行getMethod
			int statusCode = httpClient.executeMethod(getMethod);
			if (statusCode != 200) {
				System.err.println("Method failed: "
						+ getMethod.getStatusLine());
			}
			// 读取内容
			byte[] responseBody = getMethod.getResponseBody();
			System.out.println(new String(responseBody));
			// 处理内容
			Map<String, Object> map = Maps.newHashMap();
			map.put("msg", new String(responseBody));
			return new ResponseEntity(map,HttpStatus.OK);
		} catch (HttpException e) {
			// 发生致命的异常，可能是协议不对或者返回的内容有问题
			System.out.println();
			e.printStackTrace();
			return new ResponseEntity("Please check your provided http address!",HttpStatus.BAD_REQUEST);
		} catch (IOException e) {
			// 发生网络异常
			e.printStackTrace();
			return new ResponseEntity("Time out!",HttpStatus.BAD_GATEWAY);
		} finally {
			// 释放连接
			getMethod.releaseConnection();
		}
		
	}
	
	@RequestMapping(value = "/get_access_token", method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> weiboGetUserInfo(HttpServletRequest request) throws ClassNotFoundException {

		String url = request.getParameter("url");
		System.out.println(url);
		HttpClient httpClient = new HttpClient();
		// 创建POST方法的实例
		PostMethod postMethod = new PostMethod(url);
		// 使用系统提供的默认的恢复策略
		postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
				new DefaultHttpMethodRetryHandler());
		try {
			// 执行postMethod
			int statusCode = httpClient.executeMethod(postMethod);
			if (statusCode != 200) {
				System.err.println("Method failed: "
						+ postMethod.getStatusLine());
			}
			// 读取内容
			byte[] responseBody = postMethod.getResponseBody();
			System.out.println(new String(responseBody));
			// 处理内容
			Map<String, Object> map = Maps.newHashMap();
			map.put("msg", new String(responseBody));
			return new ResponseEntity(map,HttpStatus.OK);
		} catch (HttpException e) {
			// 发生致命的异常，可能是协议不对或者返回的内容有问题
			System.out.println();
			e.printStackTrace();
			return new ResponseEntity("Please check your provided http address!",HttpStatus.BAD_REQUEST);
		} catch (IOException e) {
			// 发生网络异常
			e.printStackTrace();
			return new ResponseEntity("Time out!",HttpStatus.BAD_GATEWAY);
		} finally {
			// 释放连接
			postMethod.releaseConnection();
		}
		
	}
}
